package com.wlyy.bcwlw.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

import com.wlyy.bcwlw.common.exception.DateFormatException;


public abstract class DateUtil {
	private static Map<String, String> regexp2DateFormatPattern = new HashMap<String, String>();
	static{
		regexp2DateFormatPattern.put("^\\d{6}$", "yyyyMM");
		regexp2DateFormatPattern.put("^\\d{4}-\\d{2}$", "yyyy-MM");
		regexp2DateFormatPattern.put("^\\d{8}$", "yyyyMMdd");
		regexp2DateFormatPattern.put("^\\d{4}-\\d{2}-\\d{2}$", "yyyy-MM-dd");
		regexp2DateFormatPattern.put("^\\d{4}-\\d{2}-\\d{2} \\d{2}$", "yyyy-MM-dd HH");
		regexp2DateFormatPattern.put("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}$", "yyyy-MM-dd HH:mm");
		regexp2DateFormatPattern.put("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$", "yyyy-MM-dd HH:mm:ss");
		regexp2DateFormatPattern.put("^\\d{8} \\d{2}$", "yyyyMMdd HH");
		regexp2DateFormatPattern.put("^\\d{8} \\d{2}:\\d{2}$", "yyyyMMdd HH:mm");
		regexp2DateFormatPattern.put("^\\d{8} \\d{2}:\\d{2}:\\d{2}$", "yyyyMMdd HH:mm:ss");
	}
	private static final String FORMAT_DATE="yyyy-MM-dd";
	
	private static final String FORMAT_DATE_TIME ="yyyy-MM-dd HH:mm:ss";
	
	/**
	 * 服务器当前时间，格式：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getCurrentTime() {
		return new SimpleDateFormat(FORMAT_DATE_TIME).format(new Date());
	}
	
	/**
	 * 服务器当前时间，格式：yyyy-MM-dd
	 * @return
	 */
	public static String getCurrentDate() {
		return new SimpleDateFormat(FORMAT_DATE).format(new Date());
	}
	
	public static String dateToStr(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}
	
	public static String dateFormatToStr(Date date){
		return dateToStr(date,FORMAT_DATE);
	}
	
	public static String dateTimeFormatToStr(Date date){
		return dateToStr(date,FORMAT_DATE_TIME);
	}
	
	/**
	 * 把格式为yyyy-mm-dd的字符串转换为Date
	 * @param str
	 * @return
	 */
	public static Date strToDate(String str) {
		Date date = null;
		try {
			date = new SimpleDateFormat(FORMAT_DATE).parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	 //用来全局控制 上一周，本周，下一周的周数变化
	public static Integer getCrossDays(String startdate, String enddate) {
		SimpleDateFormat myFormatter = new SimpleDateFormat(FORMAT_DATE);
		Long weeknum = 0L;
		try {
			java.util.Date sdate = myFormatter.parse(startdate);
			java.util.Date edate = myFormatter.parse(enddate);
			weeknum = (edate.getTime() - sdate.getTime())/ (24 * 60 * 60 * 1000);
		} catch (Exception e) {
			return 1;
		}
		return weeknum.intValue() + 1;
	}
	
	//获取一个日期距离近体啊的毫秒数
	//
	/**
	 * 获取一个日期和当前日期之间的毫秒数，如果在晚于当前日期，则这个值为负数，date的格式为yyyy-MM-dd
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static long getMillSecond(String date)throws Exception{
		SimpleDateFormat myFormatter = new SimpleDateFormat(FORMAT_DATE);
		String fromDate = date.substring(0,11);
		String nowDate = myFormatter.format(new Date());
		return getMillSecond(fromDate,nowDate);
	}
	
	//根据两个日期获得毫秒数
	//date的格式为yyyy-MM-dd
	public static long getMillSecond(String fromDate,String toDate) throws Exception{
		SimpleDateFormat myFormatter = new SimpleDateFormat(FORMAT_DATE);
		long time = myFormatter.parse(toDate).getTime()-myFormatter.parse(fromDate).getTime();
		return time;
	}
	
	//根据两个日期获得毫秒数
	//date的格式为yyyy-MM-dd hh:mm:ss
	public static long getSecond(Date nowDate, String toDate) throws ParseException{
		SimpleDateFormat myFormatter = new SimpleDateFormat(FORMAT_DATE_TIME);
		long time = nowDate.getTime() - myFormatter.parse(toDate).getTime();
		return time/1000;
	}
	
	public static boolean isGreaterThanToDate(Date nowDate,String toDate,int interval) {
		if(StringUtils.isEmpty(toDate)){
			return true;
		}
		long time=0;
		try {
			time = getSecond(nowDate,toDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return false;
		}
		if(time>interval){
			return true;
		}
		return false;
	}
	
	
	public static String getBeforeDate(String endTime,int interval){
		Date timeDate = strToDate(endTime);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(timeDate);
		calendar.add(Calendar.DAY_OF_MONTH, -interval); 
		Date beforeDate = calendar.getTime();
		return dateFormatToStr(beforeDate);
	}
	
	//获取今天据上周的天数
	public static int getLastWeekDay() throws ParseException{
		Calendar c = Calendar.getInstance();
		int weekDay = c.get(Calendar.DAY_OF_WEEK);
		int day = weekDay - 1;
		return day;
	}
	
	//获取给定日期对应的星期，date格式为 yyyy-MM-dd
	public static String getWeekDate(String date)throws Exception{
		SimpleDateFormat myFormatter = new SimpleDateFormat(FORMAT_DATE);
		Calendar c = Calendar.getInstance();
		c.setTime(myFormatter.parse(date));
		int tag = c.get(Calendar.DAY_OF_WEEK);
		Map<Integer,String> map = new HashMap<Integer,String>();
		map.put(1, "星期日");
		map.put(2, "星期一");
		map.put(3, "星期二");
		map.put(4, "星期三");
		map.put(5, "星期四");
		map.put(6, "星期五");
		map.put(7, "星期六");
		return map.get(tag);
 	} 
	//获取本周开始的日期
	public static String getWeekBeginTime()throws Exception{
		Calendar c = Calendar.getInstance();
		int day = getLastWeekDay();
		c.add(5,-day);
		return new SimpleDateFormat(FORMAT_DATE_TIME).format(c.getTime());
	}
	
	/**
	 * 根据日期字符串解析与其对应的日期格式，支持以下格式：
	 * <ul>
	 *   <li>yyyyMM</li>
	 *   <li>yyyy-MM</li>
	 *   <li>yyyyMMdd</li>
	 *   <li>yyyy-MM-dd</li>
	 *   <li>yyyy-MM-dd HH</li>
	 *   <li>yyyy-MM-dd HH:mm</li>
	 *   <li>yyyy-MM-dd HH:mm:ss</li>
	 * </ul>
	 * @param dateStr
	 * @return
	 * @throws DateFormatException 当无法解析对应的日期格式的时候
	 */
	public static DateFormat getDateFormat(String dateStr) {
		if(StringUtils.isEmpty(dateStr)) {
			return new SimpleDateFormat(FORMAT_DATE);
		}
		for(Entry<String, String> entry: regexp2DateFormatPattern.entrySet()) {
			if(RegexpUtil.isMatch(dateStr, entry.getKey())) {
				return new SimpleDateFormat(entry.getValue());
			}
		}
		throw new DateFormatException("无法解析" + dateStr + "的日期格式");
	}
	
	/**
	 * 与当前日期进行比较
	 * @param dateSrcStr 要比较的字符串，格式必须为：yyyy-MM-dd HH:mm
	 * @return 如果比当前日期早，则返回-1,如果比当前日期晚，则返回1，反之为0
	 */
	public static int dateCompare2Current(String dateSrcStr){
		
		try {
			DateFormat df = getDateFormat(dateSrcStr);
			Date dateSrc = df.parse(dateSrcStr);
			Date dateDest = df.parse(df.format(new Date()));
			return dateSrc.compareTo(dateDest);
		} catch (ParseException e) {
			throw new DateFormatException("解析" + dateSrcStr + "出错");
		}
		
	}

	/**
	 * 是否早于当前时间
	 * @param dateStr 要比较的字符串，格式必须为：yyyy-MM-dd HH:mm
	 * @return
	 */
	public static boolean isBeforeNow(String dateStr) {
		return dateCompare2Current(dateStr) < 0;
	}
	
	/**
	 * 是否晚于当前时间
	 * @param dateStr 要比较的字符串，格式必须为：yyyy-MM-dd HH:mm
	 * @return
	 */
	public static boolean isAfterNow(String dateStr) {
		return dateCompare2Current(dateStr) > 0;
	}
	
	public static String scrqFormatter(String scrq){
		if(StringUtils.isNotEmpty(scrq)){
			return scrq.length()>19?scrq.substring(0,19):scrq;
		}
		return "";
		
	}
	
	public static void main(String[] args) {
		System.out.println(dateTimeFormatToStr(new Date(1467937623000L)));
	}
}
